package com.xq.notealiyun.uitl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.xq.notealiyun.entity.AliyunNote;
import com.xq.notealiyun.entity.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @title: NoteUtile
 * @Author Tan
 * @Date: 2020/8/3 17:46
 * @Version 1.0
 */
@Component
public class NoteUtil {
    @Autowired
    private AliyunNote aliyunNote;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 返回验证码 用于前端进行判断
     * @param phoneNumber
     * @return
     */
    public String SendSms(String phoneNumber) {
        CommonResponse response = null;
        System.out.println(aliyunNote.toString());
        //随机6位数验证码
        String code = RandomCode.random();
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        String templateParam = JSON.toJSON(map).toString();

        String redisCode = (String) redisUtils.get(phoneNumber);
        if (!StringUtils.isEmpty(redisCode)) {
            //用redis的验证码覆盖存在的随机产生验证
            code=redisCode;
            System.out.println("redis中存在" + redisCode);
            return code;
        } else {
            redisUtils.set(phoneNumber, code, 60);
            DefaultProfile profile = DefaultProfile.getProfile(aliyunNote.getRegionId(), aliyunNote.getAccessKeyID(), aliyunNote.getAccessKeySecret());
            IAcsClient client = new DefaultAcsClient(profile);
            CommonRequest request = new CommonRequest();
            request.setSysMethod(MethodType.POST);
            request.setSysDomain("dysmsapi.aliyuncs.com");
            request.setSysVersion("2017-05-25");
            request.setSysAction("SendSms");
            request.putQueryParameter("RegionId", aliyunNote.getRegionId());
            request.putQueryParameter("PhoneNumbers", phoneNumber);
            request.putQueryParameter("SignName", "新冠疫情管理系统");
            request.putQueryParameter("TemplateCode", aliyunNote.getTemplateCode());
            request.putQueryParameter("TemplateParam", templateParam);
            try {
                response = client.getCommonResponse(request);
                System.out.println(response.getData());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            //将返回的JSON数据转成对象 然后进行判断
            ResponseData responseData = JSON.parseObject(response.getData(), ResponseData.class);
            if(responseData.getCode().equals("OK")){
                return code;
            }else {
                System.out.println("出问题了："+responseData.getCode());
                return "报错";
            }

        }
    }
}